<template>
	<view class="content">
		<view class="text">发送邮件</view>
		<view class="box">
			<view class="left">
				<text>邮箱</text>
				<text>标题</text>
				<text>内容</text>
			</view>
			<view class="right">
				<uni-forms ref="form" :rules="rules" :modelValue="email" label-width=10>
					<uni-forms-item name="mail">
						<uni-easyinput v-model="email.mail" placeholder="请输入需要发送的邮箱" prefixIcon="mail-open-filled">
						</uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="title">
						<uni-easyinput v-model="email.title" placeholder="请输入邮件标题" prefixIcon="contact-filled">
						</uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="content">
						<uni-easyinput type="textarea" v-model="email.content" placeholder="请输入邮件内容"></uni-easyinput>
					</uni-forms-item>
				</uni-forms>
			</view>

		</view>

		<button @click="fasong('form')" class="button">发送邮件</button>
	</view>
</template>
<script>
	import { mapState} from 'vuex'
	export default {
		data() {
			return {
				email: {
					content: "142342355",
					mail: '1810649011@qq.com',
					title: '12314124'
				},
				rules: {
					mail: {
						rules: [{
							required: true,
							errorMessage: '邮箱地址不能为空',
						}, {
							format: 'email',
							errorMessage: '请输入正确的邮箱地址',
						}],
						validateTrigger: 'submit'
					},
					title: {
						rules: [{
							required: true,
							errorMessage: '标题不能为空',
						}],
						validateTrigger: 'submit'
					},
					content: {
						rules: [{
							required: true,
							errorMessage: '邮件内容不能为空',
						}],
						validateTrigger: 'submit'
					}
				}
			};
		},
		onLoad() {
 
		},
		computed: {
			...mapState('m_user', ['token'])
		},
		methods: {
			fasong(form) {
				this.$refs.form.validate().then(res => {
					uni.request({
						url: 'https://api.apiopen.top/api/sendMail', //仅为示例，并非真实接口地址。
						method: 'POST',
						data: this.email,
						header: {
							token:this.token
						},
						success: (res) => {
							if (res.data.code !== 200) {
								uni.$showMsg(res.data.message)
							} else {
								uni.showModal({
									title: '邮件',
									content: '是否发送邮件',
									success: function(res) {
										if (res.confirm) {
											uni.showLoading({
												title: '正在发送'
											});
											setTimeout(function () {
												uni.hideLoading();
												uni.$showMsg("已发送到邮箱")
											}, 1000);
										} else if (res.cancel) {
											uni.$showMsg("已取消发送")
										}
									}
								});
								this.email.content = ""
								this.email.title = ""
							}
						}
					});
				}).catch(err => {
					console.log('表单错误信息：', err);
				})

			}
		},
		
	}
</script>
<style lang="scss">
	page {
		background: #7b7b7b;
	}

	.content {
		text-align: center;
		margin: 200rpx auto;
		width: 90%;
		background-color: rgba(0, 0, 0, 0.2);
		padding: 20rpx;
		border-radius: 10px;

		.button {
			color: white;
			background-color: greenyellow;
			width: 100px;
			height: 30px;
			font-size: 15px;
			text-align: center;
			line-height: 30px;
			font-weight: bold;
			margin-bottom: 10px;
		}

		.text {
			color: white;
			font-weight: bold;
			margin-bottom: 20px;
			margin-top: 20px;
		}

		.box {
			display: flex;

			.left {
				margin-top: 10px;
				color: white;
				// background-color: yellow;
				flex-direction: column;
				justify-content: space-between;
				display: flex;
				width: 25px;
				height: 134px;
				flex-wrap: wrap;
				font-size: 12px;
				margin-right: 20px;
			}

			.right {
				flex: 1;
			}
		}
	}
</style>
